package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Set;

/**
 * @author heixiaolv.com
 * @date 2021/12/3 21:29
 */
public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg(){
        System.out.println("clearImgING.......");

        Set<String> set = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);

        if (set != null && set.size() > 0){
            for (String picImgName : set) {
                //删除七牛云服务器上的图片
                QiniuUtils.deleteFileFromQiniu(picImgName);

                //删除redis中的图片缓存信息
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,picImgName);
                System.out.println("从redis中删除了 " + picImgName);
            }
        }
    }

    public void clearAllImg(){
        System.out.println("clearAllImgING......." + new Date());

        jedisPool.getResource().del(RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        jedisPool.getResource().del(RedisConstant.SETMEAL_PIC_RESOURCES);

        System.out.println("清空redis集合完毕....." + new Date());
    }
}
